#define _CRT_SECURE_NO_WARNINGS
/**
 * 校园智能快递集散系统
 * 程序设计基础课程设计(2024级)
 *
 * 本文件包含通知系统控制台界面实现
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include "notification_system_UI.h"
#include "noticement_system.h"
#include "system_types.h"


 // 控制台绘制常量
#define CONSOLE_WIDTH 120
#define CONSOLE_HEIGHT 30

#define MAX_NOTIFICATIONS_PER_PAGE 10
#define PREVIEW_LENGTH 50

// 绘制通知控制台界面框架
void drawNotificationConsoleFrame(const char* title) {
    int titleLen = strlen(title);
    int padding = (CONSOLE_WIDTH - titleLen - 4) / 2;

    printf("\n");

    // 上边框
    for (int i = 0; i < CONSOLE_WIDTH; i++) {
        printf("-");
    }
    printf("\n");

    // 标题行
    printf("|");
    for (int i = 0; i < padding; i++) {
        printf(" ");
    }
    printf(" %s ", title);
    for (int i = 0; i < padding + (titleLen % 2 == 0 ? 0 : 1); i++) {
        printf(" ");
    }
    printf("|\n");

    // 下边框
    for (int i = 0; i < CONSOLE_WIDTH; i++) {
        printf("-");
    }
    printf("\n");
}

// 格式化通知时间
void formatNotificationTime(time_t createTime, char* buffer, size_t bufferSize) {
    struct tm* timeInfo;
    timeInfo = localtime(&createTime);

    if (timeInfo == NULL) {
        strncpy(buffer, "Unknown time", bufferSize);
        return;
    }

    // 获取当前时间
    time_t now = time(NULL);
    struct tm* nowInfo = localtime(&now);

    if (nowInfo == NULL) {
        strftime(buffer, bufferSize, "%Y-%m-%d %H:%M", timeInfo);
        return;
    }

    // 判断是否是今天
    if (timeInfo->tm_year == nowInfo->tm_year &&
        timeInfo->tm_mon == nowInfo->tm_mon &&
        timeInfo->tm_mday == nowInfo->tm_mday) {
        // 今天
        strftime(buffer, bufferSize, "今天 %H:%M", timeInfo);
    }
    // 判断是否是昨天
    else if (timeInfo->tm_year == nowInfo->tm_year &&
        timeInfo->tm_mon == nowInfo->tm_mon &&
        timeInfo->tm_mday == nowInfo->tm_mday - 1) {
        // 昨天
        strftime(buffer, bufferSize, "昨天 %H:%M", timeInfo);
    }
    // 判断是否是本周
    else if (now - createTime < 7 * 24 * 60 * 60) {
        // 本周
        char* weekdays[] = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
        snprintf(buffer, bufferSize, "%s %02d:%02d",
            weekdays[timeInfo->tm_wday],
            timeInfo->tm_hour,
            timeInfo->tm_min);
    }
    // 判断是否是今年
    else if (timeInfo->tm_year == nowInfo->tm_year) {
        // 今年
        strftime(buffer, bufferSize, "%m-%d %H:%M", timeInfo);
    }
    else {
        // 往年
        strftime(buffer, bufferSize, "%Y-%m-%d %H:%M", timeInfo);
    }
}

// 生成通知内容预览
char* getNotificationPreview(Notification* notification, int maxLength) {
    if (notification == NULL || notification->message == NULL) {
        return NULL;
    }

    int msgLen = strlen(notification->message);
    int previewLen = (msgLen < maxLength) ? msgLen : maxLength;

    char* preview = (char*)malloc(previewLen + 4); // +4 for "..." and null terminator
    if (preview == NULL) {
        return NULL;
    }

    strncpy(preview, notification->message, previewLen);
    preview[previewLen] = '\0';

    if (msgLen > maxLength) {
        strcat(preview, "...");
    }

    return preview;
}

// 检查用户是否有通知访问权限
int hasNotificationAccess(Notification* notification, User* user) {
    if (notification == NULL || user == NULL) {
        return 0;
    }

    // 管理员有权限访问所有通知
    if (user->type == USER_ADMIN) {
        return 1;
    }

    // 用户只能访问自己的通知
    if (strcmp(notification->receiverPhone, user->phone) == 0) {
        return 1;
    }

    return 0;
}

// 获取用户未读通知数量
int getUnreadNotificationCount(SystemData* data, User* currentUser) {
    if (data == NULL || currentUser == NULL) {
        return 0;
    }

    int count = 0;

    // 遍历用户通知链表
    Notification* current = data->notificationList;
    while (current != NULL) {
        if (strcmp(current->receiverPhone, currentUser->phone) == 0) {
            if (current->isRead == 0) {
                count++;
            }
        }
        current = current->next;
    }

    return count;
}

// 显示用户通知控制台界面
ErrorCode showNotificationConsole(SystemData* data, User* currentUser) {
    if (data == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }

    int choice = 0;
    int filterType = 0;  // 默认显示所有类型
    int readStatus = 0;  // 默认显示所有状态
    int currentPage = 1;
    int running = 1;
    int unreadCount = getUnreadNotificationCount(data, currentUser);

    while (running) {
        system("cls");  // 清屏，Windows系统
        // 在Linux系统上使用: system("clear");

        drawNotificationConsoleFrame("我的通知");

        printf("\n当前用户: %s\n", currentUser->username);
        printf("未读通知: %d 条\n\n", unreadCount);

        // 显示通知列表
        displayUserNotifications(data, currentUser, filterType, readStatus,
            MAX_NOTIFICATIONS_PER_PAGE, &currentPage);

        printf("\n--- 操作菜单 ---\n");
        printf("1. 查看通知详情\n");
        printf("2. 标记为已读/未读\n");
        printf("3. 全部标记为已读\n");
        printf("4. 删除通知\n");
        printf("5. 筛选通知类型\n");
        printf("6. 筛选通知状态\n");
        printf("7. 搜索通知\n");
        printf("8. 翻页\n");
        printf("9. 返回主菜单\n");

        if (currentUser->type == USER_ADMIN) {
            printf("0. 通知管理(管理员)\n");
        }

        printf("\n请选择操作 [1-9]: ");
        scanf("%d", &choice);
        getchar();  // 吸收换行符

        char notificationID[MAX_NOTIFICATION_ID_LEN];
        char keyword[MAX_DESCRIPTION_LEN];

        switch (choice) {
        case 1:  // 查看通知详情
            printf("请输入通知ID: ");
            scanf("%s", notificationID);
            getchar();  // 吸收换行符
            viewNotificationDetail(data, notificationID, currentUser);
            printf("\n按回车键继续...");
            getchar();
            break;

        case 2:  // 标记为已读/未读
            printf("请输入通知ID: ");
            scanf("%s", notificationID);
            getchar();  // 吸收换行符

            printf("标记为: 1.已读 0.未读: ");
            int markAsRead;
            scanf("%d", &markAsRead);
            getchar();  // 吸收换行符

            ErrorCode result = toggleNotificationReadStatus(data, notificationID, markAsRead, currentUser);
            if (result == OK) {
                printf("标记成功!\n");
                // 更新未读数量
                unreadCount = getUnreadNotificationCount(data, currentUser);
            }
            else {
                printf("操作失败，错误码: %d\n", result);
            }

            printf("\n按回车键继续...");
            getchar();
            break;

        case 3:  // 全部标记为已读
            printf("确定标记所有通知为已读? (1.是 0.否): ");
            int confirm;
            scanf("%d", &confirm);
            getchar();  // 吸收换行符

            if (confirm) {
                ErrorCode result = markAllNotificationsAsRead(data, currentUser, filterType);
                if (result == OK) {
                    printf("所有通知已标记为已读!\n");
                    unreadCount = 0;
                }
                else {
                    printf("操作失败，错误码: %d\n", result);
                }
            }

            printf("\n按回车键继续...");
            getchar();
            break;

        case 4:  // 删除通知
            printf("请输入通知ID (或输入ALL删除全部): ");
            char deleteInput[10];
            scanf("%s", deleteInput);
            getchar();  // 吸收换行符

            if (strcmp(deleteInput, "ALL") == 0) {
                printf("确定删除所有通知? (1.是 0.否): ");
                int confirmDelete;
                scanf("%d", &confirmDelete);
                getchar();  // 吸收换行符

                if (confirmDelete) {
                    ErrorCode result = deleteAllNotifications(data, currentUser, readStatus);
                    if (result == OK) {
                        printf("所有通知已删除!\n");
                        currentPage = 1;  // 重置页码
                        unreadCount = getUnreadNotificationCount(data, currentUser);
                    }
                    else {
                        printf("操作失败，错误码: %d\n", result);
                    }
                }
            }
            else {
                ErrorCode result = deleteNotification(data, deleteInput, currentUser);
                if (result == OK) {
                    printf("通知已删除!\n");
                    unreadCount = getUnreadNotificationCount(data, currentUser);
                }
                else {
                    printf("操作失败，错误码: %d\n", result);
                }
            }

            printf("\n按回车键继续...");
            getchar();
            break;

        case 5:  // 筛选通知类型
            printf("选择通知类型筛选:\n");
            printf("0. 全部通知\n");
            printf("1. 包裹到达\n");
            printf("2. 包裹已取\n");
            printf("3. 取件提醒\n");
            printf("4. 包裹损坏\n");
            printf("5. 包裹丢失\n");
            printf("6. 包裹退回\n");
            printf("7. 错误取件\n");
            printf("8. 系统通知\n");
            printf("请选择: ");

            scanf("%d", &filterType);
            getchar();  // 吸收换行符
            currentPage = 1;  // 重置页码
            break;

        case 6:  // 筛选通知状态
            printf("选择通知状态筛选:\n");
            printf("0. 全部\n");
            printf("1. 未读\n");
            printf("2. 已读\n");
            printf("请选择: ");

            scanf("%d", &readStatus);
            getchar();  // 吸收换行符
            currentPage = 1;  // 重置页码
            break;

        case 7:  // 搜索通知
            printf("请输入搜索关键词: ");
            scanf("%s", keyword);
            getchar();  // 吸收换行符
            searchUserNotifications(data, currentUser, keyword);
            printf("\n按回车键继续...");
            getchar();
            break;

        case 8:  // 翻页
            printf("当前页: %d\n", currentPage);
            printf("1. 下一页\n");
            printf("2. 上一页\n");
            printf("3. 跳转到指定页\n");
            printf("请选择: ");

            int pageOp;
            scanf("%d", &pageOp);
            getchar();  // 吸收换行符

            if (pageOp == 1) {
                currentPage++;
            }
            else if (pageOp == 2 && currentPage > 1) {
                currentPage--;
            }
            else if (pageOp == 3) {
                printf("请输入页码: ");
                scanf("%d", &currentPage);
                getchar();  // 吸收换行符
                if (currentPage < 1) currentPage = 1;
            }
            break;

        case 9:  // 返回主菜单
            running = 0;
            break;

        case 0:  // 通知管理(管理员)
            if (currentUser->type == USER_ADMIN) {
                notificationAdminConsole(data, currentUser);
            }
            else {
                printf("权限不足，无法访问管理功能!\n");
                printf("\n按回车键继续...");
                getchar();
            }
            break;

        default:
            printf("无效选择，请重试!\n");
            printf("\n按回车键继续...");
            getchar();
        }
    }

    return OK;
}

// 显示用户所有通知列表
ErrorCode displayUserNotifications(SystemData* data, User* currentUser,
    int filterType, int readStatus,
    int pageSize, int* currentPage) {
    if (data == NULL || currentUser == NULL || pageSize <= 0 || currentPage == NULL) {
        return ERR_INVALID_PARAM;
    }

    if (*currentPage < 1) {
        *currentPage = 1;
    }

    // 获取用户通知列表
    Notification* notifications[1000]; // 假设最多1000条通知
    int count = 0;

    // 从用户通知链表获取通知
    Notification* current = data->notificationList;
    while (current != NULL) {
        if (strcmp(current->receiverPhone, currentUser->phone && count < 1000) == 0) {
            // 应用过滤条件
            int typeMatch = (filterType == 0 || current->notificationType == filterType);
            int statusMatch = (readStatus == 0 ||
                (readStatus == 1 && current->isRead == 0) ||
                (readStatus == 2 && current->isRead == 1));

            if (typeMatch && statusMatch) {
                notifications[count++] = current;
            }
        }

        current = current->next;
    }

    // 计算总页数
    int totalPages = (count + pageSize - 1) / pageSize;
    if (totalPages == 0) totalPages = 1;

    // 确保当前页在有效范围内
    if (*currentPage > totalPages) {
        *currentPage = totalPages;
    }

    // 计算当前页的起始和结束索引
    int startIdx = (*currentPage - 1) * pageSize;
    int endIdx = startIdx + pageSize;
    if (endIdx > count) endIdx = count;

    // 显示通知列表
    printf("\n--- 通知列表 (第 %d/%d 页) ---\n", *currentPage, totalPages);
    printf("%-12s | %-15s | %-10s | %-6s | %s\n",
        "通知ID", "时间", "类型", "状态", "内容预览");
    printf("----------------------------------------------------------------------------\n");

    if (count == 0) {
        printf("                          没有符合条件的通知                              \n");
    }
    else {
        for (int i = startIdx; i < endIdx; i++) {
            Notification* noti = notifications[i];

            char timeStr[20];
            formatNotificationTime(noti->createTime, timeStr, sizeof(timeStr));

            char* preview = getNotificationPreview(noti, PREVIEW_LENGTH);

            printf("%-12s | %-15s | %-10s | %-6s | %s\n",
                noti->notificationID,
                timeStr,
                getNotificationTypeString(noti->notificationType),
                noti->isRead ? "已读" : "未读",
                preview != NULL ? preview : "无法显示内容");

            free(preview);
        }
    }

    printf("----------------------------------------------------------------------------\n");

    return OK;
}

// 查看通知详情
ErrorCode viewNotificationDetail(SystemData* data, const char* notificationID, User* currentUser) {
    if (data == NULL || notificationID == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }

    // 查找通知
    Notification* notification = findNotificationByID(data, notificationID);
    if (notification == NULL) {
        printf("未找到通知: %s\n", notificationID);
        return ERR_NOT_FOUND;
    }

    // 检查权限
    if (!hasNotificationAccess(notification, currentUser)) {
        printf("您没有权限查看此通知!\n");
        return ERR_NO_PERMISSION;
    }

    // 显示通知详情
    system("cls");  // 清屏，Windows系统
    // 在Linux系统上使用: system("clear");

    drawNotificationConsoleFrame("通知详情");

    char timeStr[64];
    struct tm* timeInfo = localtime(&notification->createTime);
    strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", timeInfo);

    printf("\n通知ID: %s\n", notification->notificationID);
    printf("接收者: %s\n", notification->receiverPhone);
    printf("类型: %s\n", getNotificationTypeString(notification->notificationType));
    printf("创建时间: %s\n", timeStr);
    printf("状态: %s\n", notification->isRead ? "已读" : "未读");
    printf("\n--- 通知内容 ---\n");
    printf("%s\n", notification->message);
    printf("\n");

    // 如果通知未读，标记为已读
    if (notification->isRead == 0) {
        ErrorCode result = markNotificationAsRead(data, notificationID);
        if (result == OK) {
            printf("(已自动标记为已读)\n");
        }
    }

    return OK;
}

// 标记通知为已读/未读
ErrorCode toggleNotificationReadStatus(SystemData* data, const char* notificationID,
    int markAsRead, User* currentUser) {
    if (data == NULL || notificationID == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }

    // 查找通知
    Notification* notification = findNotificationByID(data, notificationID);
    if (notification == NULL) {
        return ERR_NOT_FOUND;
    }

    // 检查权限
    if (!hasNotificationAccess(notification, currentUser)) {
        return ERR_NO_PERMISSION;
    }

    // 更新状态
    if (markAsRead) {
        return markNotificationAsRead(data, notificationID);
    }
    else {
        // 标记为未读
        notification->isRead = 0;
        return OK;
    }
}

// 批量标记通知为已读
ErrorCode markAllNotificationsAsRead(SystemData* data, User* currentUser, int type) {
    if (data == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }

    // 遍历用户通知链表
    Notification* current = data->notificationList;
    while (current != NULL) {
        if (strcmp(current->receiverPhone, currentUser->phone) == 0) {
            // 如果类型匹配或者类型为0(全部)
            if (type == 0 || current->notificationType == type) {
                current->isRead = 1;
            }
        }
        current = current->next;
    }

    return OK;
}

// 删除通知
ErrorCode deleteNotification(SystemData* data, const char* notificationID, User* currentUser) {
    if (data == NULL || notificationID == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }

    // 查找通知
    Notification* notification = findNotificationByID(data, notificationID);
    if (notification == NULL) {
        return ERR_NOT_FOUND;
    }

    // 检查权限
    if (!hasNotificationAccess(notification, currentUser)) {
        return ERR_NO_PERMISSION;
    }

    // 删除通知
    return removeNotificationByID(data, notificationID);
}

// 批量删除通知
ErrorCode deleteAllNotifications(SystemData* data, User* currentUser, int readStatus) {
    if (data == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }

    // 要删除的通知ID列表
    char notificationIDs[1000][MAX_NOTIFICATION_ID_LEN];
    int count = 0;

    // 遍历用户通知链表，找出要删除的通知
    Notification* current = data->notificationList;
    while (current != NULL && count < 1000) {
        if (strcmp(current->receiverPhone, currentUser->phone) == 0) {
            // 如果状态匹配或者状态为0(全部)
            if (readStatus == 0 ||
                (readStatus == 1 && current->isRead == 0) ||
                (readStatus == 2 && current->isRead == 1)) {

                strcpy(notificationIDs[count++], current->notificationID);
            }
        }
        current = current->next;
    }

    // 删除收集到的通知
    for (int i = 0; i < count; i++) {
        removeNotificationByID(data, notificationIDs[i]);
    }

    return OK;
}

// 搜索用户通知
ErrorCode searchUserNotifications(SystemData* data, User* currentUser, const char* keyword) {
    if (data == NULL || currentUser == NULL || keyword == NULL) {
        return ERR_INVALID_PARAM;
    }

    system("cls");  // 清屏，Windows系统
    // 在Linux系统上使用: system("clear");

    drawNotificationConsoleFrame("搜索结果");

    printf("\n搜索关键词: \"%s\"\n\n", keyword);

    // 找出匹配的通知
    printf("%-12s | %-15s | %-10s | %-6s | %s\n",
        "通知ID", "时间", "类型", "状态", "内容预览");
    printf("----------------------------------------------------------------------------\n");

    int found = 0;
    Notification* current = data->notificationList;

    while (current != NULL) {
        if (strcmp(current->receiverPhone, currentUser->phone) == 0) {
            // 在通知内容中搜索关键词
            if (strstr(current->message, keyword) != NULL) {
                found++;

                char timeStr[20];
                formatNotificationTime(current->createTime, timeStr, sizeof(timeStr));

                char* preview = getNotificationPreview(current, PREVIEW_LENGTH);

                printf("%-12s | %-15s | %-10s | %-6s | %s\n",
                    current->notificationID,
                    timeStr,
                    getNotificationTypeString(current->notificationType),
                    current->isRead ? "已读" : "未读",
                    preview != NULL ? preview : "无法显示内容");

                free(preview);
            }
        }

        current = current->next;
    }

    printf("----------------------------------------------------------------------------\n");

    if (found == 0) {
        printf("                       没有找到匹配的通知                               \n");
    }
    else {
        printf("找到 %d 条匹配的通知\n", found);
    }

    return OK;
}

// 显示通知统计信息
ErrorCode showNotificationStatistics(SystemData* data, User* currentUser) {
    if (data == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }

    system("cls");  // 清屏，Windows系统
    // 在Linux系统上使用: system("clear");

    drawNotificationConsoleFrame("通知统计");

    // 统计各类型通知数量
    int typeCount[9] = { 0 };  // 索引0未使用，1-8对应通知类型
    int totalUnread = 0;
    int totalRead = 0;

    Notification* current = data->notificationList;
    while (current != NULL) {
        if (strcmp(current->receiverPhone, currentUser->phone) == 0) {
            if (current->notificationType >= 1 && current->notificationType <= 8) {
                typeCount[current->notificationType]++;
            }

            if (current->isRead) {
                totalRead++;
            }
            else {
                totalUnread++;
            }
        }

        current = current->next;
    }

    int total = totalRead + totalUnread;

    // 显示统计信息
    printf("\n用户: %s\n", currentUser->username);
    printf("总通知数: %d\n", total);
    printf("已读: %d (%.1f%%)\n", totalRead, total > 0 ? (totalRead * 100.0 / total) : 0);
    printf("未读: %d (%.1f%%)\n\n", totalUnread, total > 0 ? (totalUnread * 100.0 / total) : 0);

    printf("通知类型分布:\n");
    for (int i = 1; i <= 8; i++) {
        printf("- %-12s: %d (%.1f%%)\n",
            getNotificationTypeString(i),
            typeCount[i],
            total > 0 ? (typeCount[i] * 100.0 / total) : 0);
    }

    return OK;
}

// 通知管理菜单(管理员用)
ErrorCode notificationAdminConsole(SystemData* data, User* currentUser) {
    if (data == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }

    // 检查权限
    if (currentUser->type != USER_ADMIN) {
        printf("权限不足，无法访问管理功能!\n");
        return ERR_NO_PERMISSION;
    }

    int choice = 0;
    int running = 1;

    while (running) {
        system("cls");  // 清屏，Windows系统
        // 在Linux系统上使用: system("clear");

        drawNotificationConsoleFrame("通知管理");

        printf("\n--- 管理员功能 ---\n");
        printf("1. 发送系统通知\n");
        printf("2. 清理过期通知\n");
        printf("3. 生成取件提醒\n");
        printf("4. 通知统计报告\n");
        printf("5. 返回通知主界面\n");

        printf("\n请选择操作 [1-5]: ");
        scanf("%d", &choice);
        getchar();  // 吸收换行符

        char receiverPhone[MAX_PHONE_LEN];
        char message[MAX_DESCRIPTION_LEN];
        int days;

        switch (choice) {
        case 1:  // 发送系统通知
            printf("接收者手机号 (直接回车发送给所有用户): ");
            fgets(receiverPhone, MAX_PHONE_LEN, stdin);

            // 移除换行符
            receiverPhone[strcspn(receiverPhone, "\n")] = 0;

            printf("通知内容: ");
            fgets(message, MAX_DESCRIPTION_LEN, stdin);

            // 移除换行符
            message[strcspn(message, "\n")] = 0;

            ErrorCode result = sendSystemNotification(data,
                strlen(receiverPhone) > 0 ? receiverPhone : NULL,
                message,
                currentUser);

            if (result == OK) {
                printf("系统通知已发送!\n");
            }
            else {
                printf("发送失败，错误码: %d\n", result);
            }

            printf("\n按回车键继续...");
            getchar();
            break;

        case 2:  // 清理过期通知
            printf("清理多少天前的通知: ");
            scanf("%d", &days);
            getchar();  // 吸收换行符

            if (days > 0) {
                ErrorCode result = cleanupExpiredNotification(data, days);
                if (result == OK) {
                    printf("过期通知已清理!\n");
                }
                else {
                    printf("清理失败，错误码: %d\n", result);
                }
            }
            else {
                printf("天数必须大于0!\n");
            }

            printf("\n按回车键继续...");
            getchar();
            break;

        case 3:  // 生成取件提醒
            printf("为超过多少天未取的包裹生成提醒: ");
            scanf("%d", &days);
            getchar();  // 吸收换行符

            if (days > 0) {
                ErrorCode result = generatePickupReminders(data, days);
                if (result == OK) {
                    printf("取件提醒已生成!\n");
                }
                else {
                    printf("生成失败，错误码: %d\n", result);
                }
            }
            else {
                printf("天数必须大于0!\n");
            }

            printf("\n按回车键继续...");
            getchar();
            break;

        case 4:  // 通知统计报告
            // 这里应该显示全系统的通知统计信息
            printf("功能尚未实现\n");
            printf("\n按回车键继续...");
            getchar();
            break;

        case 5:  // 返回通知主界面
            running = 0;
            break;

        default:
            printf("无效选择，请重试!\n");
            printf("\n按回车键继续...");
            getchar();
        }
    }

    return OK;
}

// 创建并发送系统通知
ErrorCode sendSystemNotification(SystemData* data, const char* receiverPhone,
    const char* message, User* currentUser) {
    if (data == NULL || message == NULL || currentUser == NULL) {
        return ERR_INVALID_PARAM;
    }

    // 检查权限
    if (currentUser->type != USER_ADMIN) {
        return ERR_NO_PERMISSION;
    }

    if (receiverPhone == NULL) {
        // 发送给所有用户
        // 这里需要遍历用户列表，为每个用户创建通知
        User* user = data->userList;
        while (user != NULL) {
            // 创建通知
            Notification* notification = generateNotification(NOTIFICATION_SYSTEM,
                user->phone,
                NULL);
            if (notification == NULL) {
                return ERR_MEMORY;
            }

            // 设置消息内容
            strncpy(notification->message, message, MAX_DESCRIPTION_LEN);

            // 添加到系统
            ErrorCode result = addNotification(data, notification);
            if (result != OK) {
                free(notification);
                return result;
            }

            user = user->next;
        }
    }
    else {
        // 发送给特定用户
        Notification* notification = generateNotification(NOTIFICATION_SYSTEM,
            receiverPhone,
            NULL);
        if (notification == NULL) {
            return ERR_MEMORY;
        }

        // 设置消息内容
        strncpy(notification->message, message, MAX_DESCRIPTION_LEN);

        // 添加到系统
        ErrorCode result = addNotification(data, notification);
        if (result != OK) {
            free(notification);
            return result;
        }
    }

    return OK;
}